package com.briup.labs.controller;

import com.briup.labs.pojo.PageBean;
import com.briup.labs.pojo.Result;
import com.briup.labs.pojo.SmartDeviceType;
import com.briup.labs.service.SmartDeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/deviceType")
public class deviceTypeController {

    @Autowired
    private SmartDeviceTypeService smartDeviceTypeService;

    @GetMapping("/all")
    public Result<List<SmartDeviceType>> all() {
        List<SmartDeviceType> list = smartDeviceTypeService.all();
        return Result.success(list);
    }

    @GetMapping("/by/condition")
    public Result<PageBean<SmartDeviceType>> list(String pageNum,
                                                  String pageSize,
                                                  @RequestParam(required = false) String key) {
        PageBean<SmartDeviceType> pageBean
                = smartDeviceTypeService.list(pageNum, pageSize, key);
        if (pageBean.getTotal() != null && pageBean.getItems() != null) {
            return Result.success(pageBean);
        } else {
            return Result.error("输出传入格式错误");
        }
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SmartDeviceType smartDeviceType) {
        smartDeviceTypeService.saveOrUpdate(smartDeviceType);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        smartDeviceTypeService.delete(id);
        return Result.success();
    }
}
